package com.jinxin.platform.cdysylives.controller;

import com.jinxin.platform.base.api.annotation.*;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.annotation.UnLoginForUser;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdysylives.bean.CdysylivesMonitorDevice;
import com.jinxin.platform.cdysylives.bean.bo.DeviceInfo;
import com.jinxin.platform.cdysylives.common.ResultEntity;
import com.jinxin.platform.cdysylives.common.SysUserVO;
import com.jinxin.platform.cdysylives.rpc.CdysyliveRpc;
import com.jinxin.platform.cdysylives.service.IEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author 那个男人
 * @Date 2023/7/25 14:50
 * @Version 1.0
 */
@APCOSApi(value = "视频设备接口", developers = {"那个男人"}, description = "视频设备接口")
@RestController
@RequestMapping("/cdysylives/monitor")
@Slf4j
public class Cdys7liveMonitorController {
    @Autowired
    private IEquipmentService equipmentService;
    @Autowired
    private CdysyliveRpc cdysyliveRpc;

    @APCOSApiOperation(value = "保存或更新视频设备", developers = {"lingnan"}, description = "保存或更新视频设备",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody CdysylivesMonitorDevice record){
        return equipmentService.saveOrUpdate(record);
    }

    @APCOSApiOperation(value = "分页查询视频设备", developers = {"lingnan"}, description = "分页查询视频设备",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/list")
    public Map<String,Object> list(@RequestParam int page, @RequestParam int limit,
                                   @RequestParam(required = false,name = "communityId")String communityId,
                                   @RequestParam(required = false,name = "buildingId")String buildingId,
                                   @RequestParam(required = false,name = "deviceName")String deviceName,
                                   @RequestParam(required = false,name = "deviceSerial")String deviceSerial,
                                   @RequestParam(required = false,name = "deviceType")String deviceType,
                                   @RequestParam(required = false,name = "installType")Integer installType,
                                   @RequestParam(required = false,name = "isEnabled")Integer isEnabled){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("communityId", communityId);
        param.put("buildingId", buildingId);
        param.put("deviceName", deviceName);
        param.put("deviceSerial", deviceSerial);
        param.put("deviceType", deviceType);
        param.put("installType", installType);
        param.put("isEnabled", isEnabled);
        Set<String> orgNameSet = cdysyliveRpc.getOrgNameSet();
        if(orgNameSet.size() == 0){
            resultMap.put("count", 0);
            resultMap.put("data", null);
            return resultMap;
        }
        param.put("orgNameSet",orgNameSet);
        resultMap.put("count", equipmentService.listPageCount(param));
        resultMap.put("data", equipmentService.listPage(param));
        return resultMap;
    }

    @APCOSApiOperation(value = "分页查询视频设备(app)", developers = {"lingnan"}, description = "分页查询视频设备(app)",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_APP)
    @GetMapping("/listForApp")
    public Map<String,Object> listForApp(@RequestParam int page, @RequestParam int limit,
                                   @RequestParam(required = false,name = "deviceName")String deviceName){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("deviceName", deviceName);
        SysUserVO currentUser = cdysyliveRpc.getCurrentUser();
        if(currentUser == null){
            resultMap.put("code",500);
            resultMap.put("msg","未登录");
            return resultMap;
        }
        String communityId = equipmentService.queryCommunityIdByUId(currentUser.getUserId());
        if(StringUtils.isEmpty(communityId)){
            resultMap.put("code",500);
            resultMap.put("msg","非物业管理人员不能查看监控");
            return resultMap;
        }
        param.put("communityId",communityId);
        resultMap.put("count", equipmentService.listPageCount(param));
        resultMap.put("data", equipmentService.listPage(param));
        return resultMap;
    }

    @APCOSApiOperation(value = "删除视频设备", developers = {"lingnan"}, description = "删除视频设备",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/delete")
    public JsonResult delete(String id){
        return equipmentService.delete(id);
    }

    @APCOSApiOperation(value = "查询楼栋列表", developers = {"lingnan"}, description = "查询楼栋列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/queryBuildingList")
    public JsonResult queryBuildingList(@RequestParam(name = "communityId") String communityId){
        return equipmentService.queryBuildingList(communityId);
    }

    @APCOSApiOperation(value = "查询设备类型列表", developers = {"lingnan"}, description = "查询设备类型列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/listDeviceType")
    public JsonResult listDeviceType(){
        return equipmentService.listDeviceType();
    }

    @APCOSApiOperation(value = "批量启用设备", developers = {"lingnan"}, description = "批量启用设备",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/batchEnable")
    public JsonResult batchEnable(@RequestBody List<String> idList){
        return equipmentService.batchEnable(idList);
    }

    @APCOSApiOperation(value = "批量禁用设备", developers = {"lingnan"}, description = "批量禁用设备",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/batchDisable")
    public JsonResult batchDisable(@RequestBody List<String> idList){
        return equipmentService.batchDisable(idList);
    }


    @APCOSApiOperation(value = "同步设备", developers = {"lingnan"}, description = "同步设备",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/syncDevice")
    public JsonResult syncDevice(@RequestParam(name = "communityId")String communityId){
        return equipmentService.syncDevice(communityId);
    }

    @APCOSApiOperation(value = "批量确认同步设备", developers = {"lingnan"}, description = "批量确认同步设备",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/batchConfirmDevice")
    public JsonResult batchConfirmDevice(@RequestBody List<DeviceInfo> deviceInfoList){
        return equipmentService.batchConfirmDevice(deviceInfoList);
    }

    @UnLoginForUser
    @APCOSApiOperation(value = "获取指定设备的通道信息", developers = {"lingnan"}, description = "获取指定设备的通道信息",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @APCOSApiRequests({
            @APCOSApiRequest(name = "deviceSerial",value = "设备序列号",required = true,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    reqType = APCOSApiConstans.REQ_TYPE_PATH,dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "32",matchType = APCOSApiConstans.MATCH_TYPE_EQ),
            @APCOSApiRequest(name = "communityId",value = "社区ID",required = true,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    reqType = APCOSApiConstans.REQ_TYPE_PATH,dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "32",matchType = APCOSApiConstans.MATCH_TYPE_EQ),
    })
    @APCOSApiResponses({
            @APCOSApiResponse(name = "code", value = "状态码0成功", required = true, fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_NUM, lenth = "5,0"),
            @APCOSApiResponse(name = "msg", value = "提示信息", required = true, fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR, lenth = "100"),
            @APCOSApiResponse(name = "data", value = "返回的通道列表", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_ARR),
            @APCOSApiResponse(name = "data.deviceSerial", value = "设备序列号", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50"),
            @APCOSApiResponse(name = "data.ipcSerial", value = "IPC序列号", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50"),
            @APCOSApiResponse(name = "data.channelNo", value = "通道号", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50"),
            @APCOSApiResponse(name = "data.deviceName", value = "设备名", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50"),
            @APCOSApiResponse(name = "data.localName", value = "设备上报名称", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50"),
            @APCOSApiResponse(name = "data.channelName", value = "通道名", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50"),
            @APCOSApiResponse(name = "data.picUrl", value = "图片地址（大图），若在萤石客户端设置封面则返回封面图片，未设置则返回默认图片", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "2000"),
            @APCOSApiResponse(name = "data.isEncrypt", value = "是否加密,0不加密,1加密", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_NUM,lenth = "1,0"),
            @APCOSApiResponse(name = "data.videoLevel", value = "视频质量:0-流畅,1-均衡,2-高清,3-超清", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_NUM,lenth = "1,0"),
            @APCOSApiResponse(name = "data.relatedIpc", value = "当前通道是否关联IPC：true-是，false-否。设备未上报或者未关联都是false", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "10"),
            @APCOSApiResponse(name = "data.isAdd", value = "0:隐藏，1:显示", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_NUM,lenth = "1,0"),
            @APCOSApiResponse(name = "data.count", value = "通道数量", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_NUM,lenth = "10,0"),
    })
    @GetMapping("/listChannelByDevice")
    public Map<String,Object> listChannelByDevice(@RequestParam(name = "deviceSerial") String deviceSerial,
                                                  @RequestParam(name = "communityId") String communityId){
        return equipmentService.listChannelByDevice(deviceSerial,communityId);
    }

    @UnLoginForUser
    @APCOSApiOperation(value = "获取播放地址", developers = {"lingnan"}, description = "获取播放地址",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @APCOSApiRequests({
            @APCOSApiRequest(name = "communityId",value = "社区ID",required = true,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    reqType = APCOSApiConstans.REQ_TYPE_PATH,dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "32",matchType = APCOSApiConstans.MATCH_TYPE_EQ),
            @APCOSApiRequest(name = "deviceSerial",value = "设备序列号",required = true,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    reqType = APCOSApiConstans.REQ_TYPE_PATH,dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "32",matchType = APCOSApiConstans.MATCH_TYPE_EQ),
            @APCOSApiRequest(name = "quality",value = "视频清晰度1-高清(主码流)2-流畅(子码流)",fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    reqType = APCOSApiConstans.REQ_TYPE_PATH,dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "10",matchType = APCOSApiConstans.MATCH_TYPE_EQ),
            @APCOSApiRequest(name = "channelNo",value = "通道号",required = true,fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    reqType = APCOSApiConstans.REQ_TYPE_PATH,dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "10",matchType = APCOSApiConstans.MATCH_TYPE_EQ),
    })
    @APCOSApiResponses({
            @APCOSApiResponse(name = "resultCode", value = "状态码000成功", required = true, fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_NUM, lenth = "5,0"),
            @APCOSApiResponse(name = "resultDesc", value = "提示信息", required = true, fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR, lenth = "100"),
            @APCOSApiResponse(name = "resultContent", value = "返回信息", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_OBJ),
            @APCOSApiResponse(name = "resultContent.token", value = "token", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "50"),
            @APCOSApiResponse(name = "resultContent.url", value = "播放地址", fieldType = APCOSApiConstans.FIELD_TYPE_GN,
                    dataType = APCOSApiConstans.DATA_TYPE_STR,lenth = "2000"),
    })
    @GetMapping("/getLiveAddress")
    public ResultEntity getLiveAddress(@RequestParam(name = "communityId") String communityId,
                                       String deviceSerial, String quality, String channelNo){
        return equipmentService.getVideoAddress(communityId,deviceSerial,quality,channelNo);
    }

    @APCOSApiOperation(value = "根据设备ID查询任务列表", developers = {"lingnan"}, description = "根据设备ID查询任务列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/listTask")
    public JsonResult listTask(@RequestParam(name = "deviceId") String deviceId){
        return equipmentService.listTask(deviceId);
    }

    @APCOSApiOperation(value = "根据任务ID获取文件列表", developers = {"lingnan"}, description = "根据任务ID获取文件列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/listFile")
    public Map<String,Object> listFile(@RequestParam(name = "taskId",required = false) String taskId,
                                       @RequestParam(name = "communityId") String communityId){
        return equipmentService.listFile(taskId,communityId);
    }

    @APCOSApiOperation(value = "删除文件", developers = {"lingnan"}, description = "删除文件",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/deleteFile")
    public JsonResult deleteFile(@RequestParam(name = "communityId") String communityId,
                                 @RequestParam(name = "fileId") String fileId,
                                       @RequestParam(name = "projectId") String projectId){
        return equipmentService.deleteFile(communityId,fileId,projectId);
    }
}
